<?php

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Venue;
use App\Type;
use App\Price;
use App\User_venue;
use App\Order;
use Illuminate\Support\Facades\DB;

class VenueController extends Controller
{


    public function order_list(Request $request){


        // 用户当前位置
        $lat = $request->lat;
        $lng = $request->lng;

        // 检索包含距离的数据集合

        $sql = "SELECT *,(
        6378.137 * 2 * ASIN(
            SQRT(
                POW(
                    SIN(
                        (
                            RADIANS(".$lat.")- RADIANS(lat)
                        )/ 2
                    ),
                    2
                )+ COS(RADIANS(".$lat."))* COS(RADIANS(lat))* POW(
                    SIN(
                        (
                            RADIANS(".$lng.")- RADIANS(lng)
                        )/ 2
                    ),
                    2
                )
            )
        )
        ) AS distance FROM venues";

                // 按距离排序并分页
                $venues = DB::table(DB::raw("($sql) as res"))->orderBy("distance")->paginate(10);

                return $venues;


    }

    // 追加模拟数据
    public function test(){

        // 北京范围内
        $latMin = "39.845588";
        $latMax = "40.051223";

        $lngMin = "116.211978";
        $lngMax = "116.568426";


        $venues = Venue::all();

        foreach($venues as $k=>$venue){

            $lng = rand($lngMin*1000000,$lngMax*1000000)/1000000;
            $lat = rand($latMin*1000000,$latMax*1000000)/1000000;

            $venue->lng = $lng;
            $venue->lat = $lat;

            $venue->save();

        }

        echo "ok";


    }


    // 关注场馆
    public function follow($id){

        $user = \Auth::guard("api")->user(); // 获取登录用户的实例对象

        $user->venues()->attach($id);        // 用户 和  场馆  对多多关联关系对象

        return response()->json(["code"=>200,"msg"=>"关注成功"]);

    }


    // 取消关注
    public function unfollow($id){

        $user = \Auth::guard("api")->user();

        $res = $user->venues()->detach($id);

        return response()->json(["code"=>200,"msg"=>"取消关注"]);

    }



    // 搜索场馆
    public function search(Request $request){
        //return $request;
        $words = $request->words;
        $venues = Venue::where("name","like","%".$words."%")->paginate(3);
        return $venues;

    }

    // 测试路由
   /* public function test(){

        $cb = $_GET["cbb"];
        $arr = Array("name"=>"tom","age"=>18);
        $json = json_encode($arr);
        echo $cb."(".$json.")";

    }*/

    // 获取场馆详情
    public function detail($venueId){
        //return $venueId;
        $venue = Venue::find($venueId);
        $venue->type_name = $venue->type->name;
        $venue->dates = $this->get_dates();
        return $venue;
    }

    // 获取日期
    private function get_dates(){

        $dates = Array();
        $week= Array("日","一","二","三","四","五","六");

        for($i=0;$i<=6;$i++){
            $dates[$i]["date"] =  date("Y-m-d",time()+ 24*60*60*$i);
            $sn = date("w",time()+ 24*60*60*$i);  // 0 ~ 6
            $dates[$i]["week"] = "星期".$week[$sn];
        }

        return $dates;

    }

    // 获取所有栏目的场馆列表
    public function list(){

        // 获取登录用户信息
        //$user = \Auth::guard("api")->user();

        $venues = Venue::orderBy("id","desc")->paginate(8);

        foreach ($venues as $k=>$venue){
            $venues[$k]["type_name"] = $venue->type->name;
            // $venues[$k]["is_followed"] = $this->isFollowed($venue->id,$user->id);
             $venues[$k]["is_followed"] = false;
        }

        return $venues;

    }


    // 判断当前时段是否被预定
    private function isFollowed($venue_id,$user_id){

        return (bool) User_venue::where("venue_id",$venue_id)
            ->where("user_id",$user_id)
            ->first();

    }

    // 获指定栏目的场馆列表
    public function venues($typeId){

        $venues = Venue::orderBy("id","desc")->where("type_id",$typeId)->paginate(8);

        foreach ($venues as $k=>$venue){
            $venues[$k]["type_name"] = $venue->type->name;
        }

        return $venues;

    }

    // 获取所有栏目内容
    public function types(){

        $types = Type::all();
        return $types;

    }

   public function date_sn_time_prices($venue_id){


        // 获取一周的日期
        $dates = $this->get_dates();
        $date_list = Array();

        for($i=0; $i<count($dates); $i++){
            $order_date = $dates[$i]["date"];
            $date_list["dates"][$order_date] = $this->sn_time_prices($venue_id,$order_date);
        }

        /*
        $date_list["2019-7-5"] = Array(
            1=>Array(9=>45,10=>45,18=>60,19=>60,22=>60),
            2=>Array(9=>45,10=>45,18=>60,19=>60,22=>60),
            3=>Array(9=>45,10=>45,18=>60,19=>60,22=>60)
        );
        */

        $date_list['times'] = $this->get_times();

        return $date_list;

    }


    // 获取当前场馆 不同场地 时间价格列表
    private function  sn_time_prices($venue_id,$order_date){
        $sn_time_prices = Array();

        $venue = Venue::find($venue_id);

        $num = $venue->num;
        for($i=1;$i<=$num;$i++){
            $sn = $i;
            $sn_time_prices[$sn] = $this->time_prices($venue_id,$order_date,$sn);
        }
        return $sn_time_prices;
    }

    // 获取每块场地  时段价格列表

    private function time_prices($venue_id,$order_date,$sn){

        //  3=>Array(9=>45,10=>45,18=>60,19=>60,22=>60);

        $time_prices = Array();

        $prices = Price::where("venue_id",$venue_id)->get();

        $prices = $prices->toArray();

        for($i=0;$i<count($prices);$i++){
            $time = $prices[$i]["time"];
            $price = $prices[$i]["price"];

            $time_prices[$time]=Array();  // Array("price"=>45,"is_ordered"=>true)
            $time_prices[$time]["price"] = $price;
            $time_prices[$time]["is_ordered"] = $this->check_orders($venue_id,$order_date,$sn,$time);
        }

        return $time_prices;


    }

    // 判断当前时段是否被预定
    private function check_orders($venue_id,$order_date,$sn,$time){

        return (bool) Order::where("venue_id",$venue_id)
            ->where("order_date",$order_date)
            ->where("sn",$sn)
            ->where("time",$time)
            ->first();

    }

    // 获取所有的时段
    private function get_times(){
        $times = Array();
        for($i=9;$i<=22;$i++){
            $times[]=$i;
        }
        return $times;
    }


}
